package mo.tags.stack;

import java.util.Stack;

public class L1003 {

    public boolean isValid(String s) {
        Stack<Character> stack = new Stack<>();
        for (char c : s.toCharArray()) {
            if (c == 'c') {
                if (stack.isEmpty() || stack.size() < 2) {
                    return false;
                }
                char pre = stack.pop();
                if (pre != 'b') {
                    return false;
                }
                pre = stack.pop();
                if (pre != 'a') {
                    return false;
                }
            } else {
                stack.push(c);
            }
        }
        return stack.isEmpty();
    }


    public static void main(String[] args) {
        L1003 test = new L1003();
        System.out.println(test.isValid("abccba"));
    }

}
